package listbyorder.access1001a.test1289;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/8/20 17:25
 */
public class Solution1 {

    public int minFallingPathSum(int[][] arr) {
        if (arr == null || arr.length == 0 || arr[0].length == 0) return 0;
        int n = arr.length;
        int res = Integer.MAX_VALUE;
        int[][] dp = new int[n][n];
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (i == n - 1) {
                    dp[i][j] = arr[i][j];
                } else {
                    dp[i][j] = Integer.MAX_VALUE;
                    for (int k = 0; k < n; k++) {
                        if (k == j) continue;
                        dp[i][j] = Math.min(dp[i + 1][k], dp[i][j]);
                    }
                    dp[i][j] += arr[i][j];
                }

                if (i == 0) {
                    res = Math.min(res, dp[i][j]);
                }
            }
        }
        return res;
    }
}
